package work.MrSun.xyq.controller;

/**
 * @Author MrSun
 * @Date 2023/4/24 18:45
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import work.MrSun.xyq.model.XyqNewMessage;
import work.MrSun.xyq.model.XyqUser;
import work.MrSun.xyq.service.XyqNewMessageService;
import work.MrSun.xyq.service.XyqUserService;

import java.util.LinkedList;
import java.util.List;

/**
 * 获取最新消息的控制层
 */
@RestController
public class GetNewMessageController {

    @Autowired
    private XyqUserService xyqUserService;

    @Autowired
    private XyqNewMessageService xyqNewMessageService;

    //获取全部的最新信息
    @PostMapping("/getMessage/getAllNewMessage/{userId}/{pageNumber}")
    public List<XyqNewMessage> getAllNewMessage(@PathVariable Integer userId,
                                                @PathVariable Integer pageNumber) {
        //根据id获取用户
        XyqUser user = xyqUserService.getById(userId);
        if (user == null) {
            return null;
        }
        //开始分页
        PageHelper.startPage(pageNumber, 10);
        //进行封装
        PageInfo<XyqNewMessage> pageInfo = new PageInfo<>(xyqNewMessageService.getAllNewMessage(userId));
        //进行判断
        if (pageInfo.getPageNum() < pageNumber) {
            //初始化数组
            List list1 = new LinkedList();
            list1.add(200);
            return list1;
        }
        //获取得到封装分页的数据
        List<XyqNewMessage> list = pageInfo.getList();
        return list;
    }

    /**
     * 根据id进行刷新出最后新消息
     * @param userId
     * @return
     */
    @PostMapping("/getMessage/getLastNewMessage/{userId}")
    public XyqNewMessage getLastNewMessage(@PathVariable Integer userId) {
        if (userId == -1) {
            return null;
        }
        return xyqNewMessageService.getLastNewMessage(userId);
    }
}
